describe("防疫-机器人信息", () => {
  beforeEach(() => {
    const username = "admin";
    const password = "123456";
    cy.login(username, password);

    cy.intercept("POST", "/api/robot-archives/v1/getCompanyRobotList").as(
      "getCompanyRobotList"
    );

    cy.visit("/disinfect/robot-info");

    cy.wait("@getCompanyRobotList");

    cy.getByData("robot-info").then(($div) => {
      expect($div.length).to.be.greaterThan(0);
    });

    openDetails();

    cy.getByData("robot-info").eq(5).click();
  });

  it("进入机器人信息-页面显示", () => {
    cy.contains("状态").should("exist");
    cy.contains("电池").should("exist");
    cy.contains("液量").should("exist");
    cy.contains("位置坐标").should("exist");
    cy.contains("无线充电").should("exist");
    cy.contains("运行速度").should("exist");
    cy.contains("设备控制").should("exist");
    cy.contains("机器人控制").should("exist");

    cy.wait("@getRobotDetails")
      .its("response.body.data")
      .then(($data) => {
        const name = $data.name;
        let abnormalState;
        if ($data.abnormalState === 1) {
          abnormalState = "异常";
        } else {
          abnormalState = "正常";
        }
        cy.getByData("details-name").should("have.text", `${name}`);
        cy.getByData("details-abnormalState").should(
          "have.text",
          `${abnormalState}`
        );
        cy.get("#position").find("canvas").should("be.visible");
      });
  });

  context("机器人控制参数", () => {
    it.only("导航下发", () => {
      robotControl(
        "导航下发",
        "/api/robot-cruise/disinfectCruiseV2",
        "disinfectCruiseV2"
      );
    });

    it("位置校准", () => {
      robotControl(
        "位置校准",
        "/api/robot-cruise/calibrationV2",
        "calibrationV2"
      );
    });
  });

  it("设备控制", () => {
    // 下拉框
    cy.contains("label", "设备状态").siblings().find("input").click();
    cy.contains("开启紫外线").click();
    cy.contains("label", "设备状态")
      .siblings()
      .find("input")
      .should("have.value", "开启紫外线");
    // 滑动条
    cy.contains("label", "雾量")
      .siblings()
      .find(".el-slider__runway")
      .trigger("mousedown", 0, 0)
      .trigger("mousemove", 70, 0)
      .trigger("mouseup");
    // 开关
    cy.contains("label", "人体保护").siblings().as("protectionSwitch");
    cy.get("@protectionSwitch").find("span").eq(0).click();
    cy.get("@protectionSwitch")
      .find("input")
      .should("have.attr", "aria-checked", "true");
    //提交
    cy.intercept(
      "POST",
      "api/robot-remote-control/v1/disinfect/controlUpperModel*"
    ).as("submitControl");

    cy.get("@protectionSwitch").find("span").contains("提交").click();

    cy.contains("警告").closest(".el-message-box").contains("确定").click();

    cy.wait("@submitControl");

    cy.contains("下发成功");
  });
});

function openDetails() {
  cy.intercept(
    "GET",
    "/api/robot-anti-epidemic-log/v1/getDisinfectStatusById/*"
  ).as("getRobotDetails");
}

function robotControl(title, api, apiName) {
  cy.intercept("GET", "/api/robot-disinfect-map/v1/list").as("getMapList");

  cy.contains(title).click();

  cy.wait("@getMapList");

  cy.contains("机器人控制");

  cy.contains("label", "选择地图").parent().find("input").click();

  cy.getByData("select-map").eq(0).click();

  cy.contains("选择坐标").parent().find("canvas").click();

  cy.intercept("POST", api).as(apiName);

  cy.contains("确定").click();

  cy.contains("警告").closest(".el-message-box").contains("确定").click();

  cy.wait("@" + apiName);

  cy.contains("操作成功");
}
